﻿using System;
using System.Collections.Generic;
using System.Linq;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using System.IO;
using NPOI.SS.UserModel;
using System.Reflection;
using NPOI.SS.Util;

namespace WorkTimePrj.Helper
{
    public class ExcelHelper
    {
        public static MemoryStream ExportExcel<T>(string strHeaderText, Dictionary<string, string> head, List<T> contentList)
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();


            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "Wicresoft";
            hssfworkbook.DocumentSummaryInformation = dsi;


            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "Wicresoft Work Stat";
            hssfworkbook.SummaryInformation = si;


            ISheet sheet = hssfworkbook.CreateSheet("Sheet1");
            ((HSSFSheet)hssfworkbook.GetSheetAt(0)).AlternativeFormula = false;
            ((HSSFSheet)hssfworkbook.GetSheetAt(0)).AlternativeExpression = false;
            sheet.DefaultColumnWidth = 20;
            int startRowIndex = 0;

            #region 表头及样式

            if (strHeaderText.Length > 0)
            {
                startRowIndex = 1;
                IRow headerRow = sheet.CreateRow(0);
                headerRow.HeightInPoints = 25;
                headerRow.CreateCell(0).SetCellValue(strHeaderText);

                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, head.Count - 1));
            }
            //headerRow.Dispose();

            #endregion
            #region head
            IRow header = sheet.CreateRow(startRowIndex);
            ICell cell = header.CreateCell(0);
            cell.SetCellValue("222");
            int i = 0;
            foreach (KeyValuePair<string, string> item in head)
            {

                cell = header.CreateCell(i);
                cell.SetCellValue(item.Key);
                i++;
            }

            Type p = typeof(T);
            PropertyInfo[] pi = p.GetProperties();
            for (int j = 0; j < contentList.Count; j++)
            {
                header = sheet.CreateRow(j + startRowIndex + 1);
                i = 0;
                foreach (KeyValuePair<string, string> pair in head)
                {
                    cell = header.CreateCell(i);
                    cell.CellStyle.Alignment = HorizontalAlignment.CENTER;

                    PropertyInfo pinfo = pi.SingleOrDefault(l => l.Name == pair.Value);
                    if (pinfo != null)
                    {
                        object o = pinfo.GetValue(contentList[j], null);
                        if (o != null)
                        {
                            cell.SetCellValue(o.ToString());
                            if (o.ToString().Length > 0)
                            {
                                if (o.ToString()[0] != '0' || o.ToString().Length == 1)
                                {
                                    double dou = 0;
                                    if (double.TryParse(o.ToString(), out dou))
                                    {
                                        cell.SetCellValue(dou);
                                    }
                                }
                            }

                        }

                    }

                    i++;
                }
            }
            #endregion


            MemoryStream file2 = new MemoryStream();
            hssfworkbook.Write(file2);
            file2.Position = 0;
            return file2;

        }
    }
}